<?php
namespace Common\Controller;
use Think\Controller;
class CoreController extends Controller {
	
    public function _initialize(){
       
        //权限检查
        $rbac=new \Org\Util\Rbac();
        $rbac::checkLogin();
        // print_r($_SESSION['_ACCESS_LIST']);
        // exit;
        // if(!$rbac::AccessDecision()){  
        //     echo '<script type="text/javascript">alert("no access");</script>';  
        //     die(); 
        // }
        //获取模板文件路径
        $this->theme = C('DEFAULT_THEME');

        //获取菜单
        $this->menuList = C('MENU_LIST');

        $nodes =array();
        foreach($_SESSION['_ACCESS_LIST'] as $fnode => $fchild){
            // $fnode = ucfirst(strtolower($fnode));
            $nodes[] = $fnode;
            if(!empty($fchild)){
                foreach ($fchild as $snode => $schild) {
                    // $snode = ucfirst(strtolower($snode));
                    if(!in_array('REPORTING', $nodes)&&$snode=='REPORTING'){
                        $nodes[] = 'REPORTING';
                    }
                    $nodes[] = '/'.$fnode.'/'.$snode;
                    if(!empty($schild)){
                        foreach ($schild as $tnode => $id) {
                            // $tnode = ucfirst(strtolower($tnode));
                            $nodes[] = '/'.$fnode.'/'.$snode.'/'.$tnode;
                        }
                    }
                }
            }
        }

        $this->assign('displayNodes',$nodes);
        $this->assign('AM',MODULE_NAME);
        //获取面包屑
        $breadcrumb = array('M'=>MODULE_NAME,'C'=>CONTROLLER_NAME,'A'=>ACTION_NAME);
        $breadcrumb = $this->getBreadcrumb($breadcrumb);
        $this->assign('breadcrumb',$breadcrumb);

        $this->logOut=U('/Admin/User/loginOut');

    }

    //读取配置中需要加载的静态文件
    public function setStaticFiles($viewType){
    	//获取内容要加载的静态文件
        $viewType = C($viewType);
        $header = $this->header;
        $footer = $this->footer;

        //设置头部加载的静态文件
        if(!empty($header['css'])){
            $header['css'] = array_merge($header['css'],$viewType['header']['css']);
            
        }else{
            $header['css'] = $viewType['header']['css'];
        }
        if(!empty($header['js'])){
            $header['js'] = array_merge($header['js'],$viewType['header']['js']);
        }else{
            $header['js'] = $viewType['header']['js'];
        }
        
        //设置底部加载的静态文件
        if(!empty($footer['css'])){
            $footer['css'] = array_merge($footer['css'],$viewType['footer']['css']);
        }else{
            $footer['css'] = $viewType['footer']['css'];
        }

        if(!empty($footer['js'])){
            $footer['js'] = array_merge($footer['js'],$viewType['footer']['js']);
        }else{
            $footer['js'] = $viewType['footer']['js'];
        }

        $this->header = $header;
        $this->footer = $footer;
    }

    //添加单独的静态文件
    public function setStaticFile($link,$position='foot',$type='css'){

        //设置头部加载的静态文件
        $header = $this->header;
        $footer = $this->footer;
        if($position=='head'){
            if($type=='css'){
                $header['css'][] = $link;
            }elseif($type=='js'){
                $header['js'][] = $link;
            }
        }elseif($position=='foot'){
            //设置底部加载的静态文件
            if($type=='css'){
                $footer['css'][] = $link;
            }elseif($type=='js'){
                $footer['js'][]=$link;
            }
        }
        $this->header = $header;
        $this->footer = $footer;

    }

    //配置面包屑
    public function getBreadcrumb($data){
    
    	$Mnode = M('node')->field('id,name,title')->where("name='".$data['M']."'")->find();
        
        if($Mnode['id']>0){
            $Cnode = M('node')->field('id,name,title')->where("name='".$data['C']."' and pid=".$Mnode['id'])->find();
        }

        if($Cnode['id']>0){
           $Anode = M('node')->field('id,name,title')->where("name='".$data['A']."' and pid=".$Cnode['id'])->find(); 
        }
    	
    	return array('M'=>$Mnode['title'],'C'=>$Cnode['title'],'A'=>$Anode['title']);
    }

    //递归处理节点列表
    public function nodeList($arr,$pid=0){  
        $nodeList=array();  
        foreach($arr as $key => $value) {  
           if($value['pid']==$pid) {  
             $value['child']=$this->nodeList($arr,$value['id']);  
             $nodeList[]=$value;  
           }  
        }
        return $nodeList;  
    }

    public function fieldVerify($Model){
        // if(IS_POST||IS_AJAX){
        
            $fieldVerify = new \Org\Util\FieldVerify;
            $fieldVerify->verifyFields($Model);
            exit;
            
        // }
    }

    public function verify($field,$attr){
        $error=array();
        if($attr['required']){
            $f = I('post.'.$field);
            $len = mb_strlen($f);
            switch ($attr['type']) {
                case 'string':
                    if(empty($f)){
                        return $field.' is required';
                    }elseif($attr['len-min']>$len||$attr['len-max']<$len){
                        return $field.' must be less than '.$attr['len-max'].' and more than'.$attr['len-min'];
                    }
                    break;
                
                default:
                    # code...
                    break;
            }
           
        }
    }
}